package service;

import com.alibaba.fastjson.JSON;
import entity.Score;
import entity.User;
import entity.UserScore;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 用户数据操作
 */
public class UserOpt {
    private static String userPath = "D:\\user.properties";
    private static String userScorePath = "D:\\userscore.properties";

    /**
     * 处理用户注册
     * @param user
     * @return
     * @throws IOException
     */
    public static boolean UserRegister(User user) throws IOException {
        Properties props = new Properties();
        props.load(new FileInputStream(new File(userPath)));
        if (props.containsKey(user.getLoginName())){
            return false;
        }
        String userInfo = JSON.toJSONString(user);
        props.setProperty(user.getLoginName(),userInfo);
        FileOutputStream fos = new FileOutputStream(userPath);
        String updMessage = "Update:"+user.getLoginName()+"--"+new Date().toString();
        props.store(fos,updMessage);
        fos.close();
        return true;
    }

    /**
     * 处理用户登录
     * @param name
     * @param password
     * @return
     * @throws IOException
     */
    public static boolean LoginCheck(String name,String password) throws IOException {
        Properties props = new Properties();
        props.load(new FileInputStream(new File(userPath)));
        if (!props.containsKey(name)){
            return false;
        }
        // 密码验证
        // fastjson将json字符串转换为JavaBean对象
        User user = JSON.parseObject(props.getProperty(name),User.class);
        if (password.equals(user.getPassword())){
            return true;
        }
        return false;
    }

    /**
     * 更新用户成绩
     * @param name
     * @param testName
     * @param score
     * @throws IOException
     */
    public static void updUserScore(String name,String testName,int score) throws IOException {
        Properties props = new Properties();
        props.load(new FileInputStream(new File(userScorePath)));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d = df.format(new Date());
        Score score1 = new Score(testName,d,score);
        if (props.containsKey(name)){
            String a = props.getProperty(name);
            UserScore userScore = JSON.parseObject(a,UserScore.class);
            List<Score> scores = userScore.getScores();
            scores.add(score1);
            userScore.setScores(scores);
            String ss = JSON.toJSONString(userScore);
            props.setProperty(name,ss);
        }else {
            UserScore userScore = new UserScore();
            List<Score> s = new ArrayList<>();
            s.add(score1);
            userScore.setLoginName(name);
            userScore.setScores(s);
            String ss = JSON.toJSONString(userScore);
            props.setProperty(name,ss);
        }

        FileOutputStream fos = new FileOutputStream(new File(userScorePath));
        String updMessage = "Update:"+name+"--"+new Date().toString();
        props.store(fos,updMessage);
        fos.close();
    }

    /**
     * 根据用户名获取用户历史成绩
     * @param name
     * @return
     * @throws IOException
     */
    public static List<Score> getUserScore(String name) throws IOException {
        Properties ps = new Properties();
        ps.load(new FileInputStream(new File(userScorePath)));
        if (!ps.containsKey(name)){
            return null;
        }
        UserScore userScore = JSON.parseObject(ps.getProperty(name),UserScore.class);
        return userScore.getScores();
    }
}
